package com.sy.core.web.system;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.sy.core.util.BaseUUID;
import com.sy.core.util.RestResult;
import com.sy.core.web.BaseCtl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ResponseBody;


import com.sy.core.model.SysRelation;
import com.sy.core.service.SysRelationService;




/**
 * 	TODO 请用一句话描述该类的作用
 * @author CaiXinChong email:1060296473@qq.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/sysRelation")
public class SysRelationCtl extends BaseCtl {

    @Autowired
    private SysRelationService  sysRelationService;
    

	@RequestMapping("/{path}")
	public ModelAndView toAnyPage(@PathVariable String path,ModelAndView mv){
	    mv.setViewName(path);
	    return mv;
    }
    
    /**
     * Action for Add
     */
    @RequestMapping(value = "/save",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult save(String str,SysRelation sysRelation){
        RestResult rr = null;
        List<SysRelation> sysRelationList = JSONObject.parseArray(str,SysRelation.class);
        for (int i = 0; i < sysRelationList.size(); i++) {
            sysRelationList.get(i).setPk(BaseUUID.getUUID());
        }
        try {
            sysRelationService.delete(sysRelationList.get(0).getRolePk());
            if(sysRelationList.get(0).getMenuPk()!=null){
                sysRelationService.insertBatch(sysRelationList);
            }
            rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            rr = new RestResult("201","FAILED");
        }
        return rr;
    }

    /**
     * Action for update
     */
    @RequestMapping(value = "/update",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult update(SysRelation sysRelation){
        RestResult rr = null;
        try {
        sysRelationService.update(sysRelation);
        rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
        e.printStackTrace();
        rr = new RestResult("201","FAILED");
        }
        return rr;
    }
    
    /**
     * Action for SelectOneByKey
     */
    @RequestMapping(value = "/query/{key}",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult queryByPrimaryKey(@PathVariable String key){
       try{
            SysRelation sysRelation = sysRelationService.queryByPrimaryKey(key);
      	 	return new RestResult("200","SUCCESS",sysRelation);
       }catch(Exception e){
    	   	logger.error("Query Model SysRelation By PrimaryKey Is Fail:"+e.getMessage());
       }
       		return new RestResult("201","FAIL");
    }
    
    /**
     * Action for SelectList
     */
    @RequestMapping(value = "/list",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult queryList(SysRelation sysRelation){
       try{
      		List<SysRelation> sysRelationList = sysRelationService.queryByCondition(sysRelation);
      	 	return new RestResult("200","SUCCESS",sysRelationList);
       }catch(Exception e){
    	   logger.error("Query Model SysRelation List Fail :"+e.getMessage());
    	   return new RestResult("201","FAIL");
       }
    }
    
    /**
     * Action for DeleteModel
     */
    @RequestMapping("/delete")
    @ResponseBody
    public RestResult delete(String pks){
        RestResult rr = null;
        try {
        sysRelationService.delete(pks);
        rr=new RestResult("200","SUCCESS");
        } catch (Exception e) {
        e.printStackTrace();
        rr = new RestResult("201","FAILED");
        }
        return rr;
    }

    /**
     *查询所有选中的menuPk
     */
    @RequestMapping(value = "/getMenuPks",produces = "application/json;charset=utf-8")
    @ResponseBody
    public RestResult getMenuPks(String rolePk){
        try{
            List<String> menuPkList = sysRelationService.queryMenuPk(rolePk);
            return new RestResult("200","SUCCESS",menuPkList);
        }catch(Exception e){
            logger.error("Query Model SysRelation List Fail :"+e.getMessage());
            return new RestResult("201","FAIL");
        }
    }
   
}
